
package com.bear.net.httpclient.util;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


import org.apache.http.protocol.HTTP;

import android.text.TextUtils;

import com.bear.net.httpclient.util.commons.codec.binary.Base64;

/**
 * 主要提供DES加密和解密方法。
 * 
 * @author suchangjun
 */
public class MyDes {

    /**
     * 本地XML存储数据加密密钥
     */
    public static final String LOCAL_KEY="dhjkj9y7";
    
    /**
     * 和服务器交互的密钥
     */
    public static final String KEY="dhjkfhy7";
    /**
     * 解密，注意密码只能是8字节，是64位(编码是utf-8)。
     * 
     * @param decrypeString 要解密的密文,不能是空。
     * @param decrypetKey 密钥8个字节，是64位。
     * @return
     * @throws Exception
     * @throws IllegalBlockSizeException
     * @throws Exception
     */
    public static String decryptDES(String decrypeString, String decrypetKey)
            throws IllegalBlockSizeException, Exception {
        
        if(TextUtils.isEmpty(decrypeString))
        {
            return null;
        }
        
        byte[] decryptedData = null;
        String decryptedUtf8 = null;
        // try {
        byte[] byteMi = Base64.decodeBase64(decrypeString);
        SecretKeySpec key = new SecretKeySpec(decrypetKey.getBytes(HTTP.UTF_8), DES);
        Cipher cipher = Cipher.getInstance(MODEL);
        cipher.init(Cipher.DECRYPT_MODE, key, IV_PARAM);
        decryptedData = cipher.doFinal(byteMi);
        decryptedUtf8 = new String(decryptedData, HTTP.UTF_8);
        // } catch (Exception e) {
        // Log.e("MyDes.java",
        // "********************* decrypt error ******************\n" +
        // e.toString());
        // }
        return decryptedUtf8;
    }

    /**
     * 加密，注意密码只能是8字节，是64位,算法是DES,模式是CBC,填充是PKCS5Padding(编码是utf-8)。
     * 
     * @param encrypeString 要加密的明文，不能是空。
     * @param encrypetKey 密钥8个字节，是64位。
     * @return
     * @throws Exception
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     * @throws Exception
     */
    public static String encryptDES(String encrypeString, String encrypetKey)
            throws IllegalBlockSizeException, BadPaddingException, Exception {
        byte[] encryptedData = null;

        IvParameterSpec zeroIv = new IvParameterSpec(IV);
        SecretKeySpec key = new SecretKeySpec(encrypetKey.getBytes(HTTP.UTF_8), DES);
        Cipher cipher = Cipher.getInstance(MODEL);
        cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
        encryptedData = cipher.doFinal(encrypeString.getBytes(HTTP.UTF_8));

        return Base64.encodeBase64String(encryptedData);
    }

    private static final String DES = "DES";

    private static final String MODEL = "DES/CBC/PKCS5Padding";

    private static final byte[] IV = {
            1, 2, 3, 4, 5, 9, 7, 8
    };// 加密的初始向量。

    private static final IvParameterSpec IV_PARAM = new IvParameterSpec(IV);

}
